﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;

namespace Backend.JYD.EFDAL.DAL
{
	public class UserInfoDAL : Backend.JYD.IDAL.IUserInfo
	{
		#region 查询数据UserInfo

		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<UserInfo> GetUserInfoByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
				IQueryable<UserInfo> query = db.UserInfoes.Where(p=>p.Status > 0);
				//其他条件代码
				return new PageInfo<UserInfo>(page, query);
			}
		}
		/// <summary>
		/// 查询数据
		/// </summary>
		public UserInfo GetUserInfoByUserId(Int64 userId)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<UserInfo> query = db.UserInfoes.Where(p => p.UserId == userId && p.Status > 0);
				return query.FirstOrDefault();
			}
		}
		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据UserInfo
		/// </summary>
		public Int64 AddUserInfo(UserInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.UserInfoes.Add(info);
				db.SaveChanges();
				return info.UserId;
			}
		}
		#endregion

		#region 修改数据UserInfo

		/// <summary>
		/// 根据字段（UserId）修改数据UserInfo
		/// </summary>
		public bool UpdateUserInfo(UserInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				var query = db.UserInfoes.Where(p => p.UserId == info.UserId);
				if (query != null)
				{
					var temp = query.FirstOrDefault();
					if (temp != null)
					{
						temp.OpId = info.OpId;
						temp.UpdateTime = DateTime.Now;
						//.......
						return db.SaveChanges() > 0;
					}
				}
				return false;
			}
		}
		#endregion

	}
}